package com.ruoyi.common.utils.image;

import com.ruoyi.common.core.domain.model.LoginUser;
import sun.misc.BASE64Decoder;

import javax.xml.bind.DatatypeConverter;
import java.io.*;
import java.util.UUID;
import java.util.Base64;

public class ImageUtil {


    /**
     * Base64转换为图片服务
     * targetPath  输出视频文件路径,不需要文件名
     * */
    public static String base64ToImg(String base64,String targetPath){


        if (base64 == null || "".equals(base64)){
            return null;
        }


        byte[] buff = DatatypeConverter.parseBase64Binary(base64);
        try {
            Base64.Decoder decoder = Base64.getDecoder();

            base64 = base64.replace("{\"file\":\"","");
            base64 = base64.replace("\"}","");
            byte[] b = decoder.decode(base64);
            // 处理数据
            for (int i = 0; i < b.length; ++i) {
                if (b[i] < 0) {
                    b[i] += 256;
                }
            }
            OutputStream out = new FileOutputStream(targetPath);
            out.write(b);
            out.flush();
            out.close();
            // 返回图片的相对路径 = 图片分类路径+图片名+图片后缀
            return targetPath;
        } catch (IOException e) {
            e.printStackTrace();

        }
        System.out.println("--------------------------------"+"图片转换完成"+"--------------------------------");
        System.out.println(base64);
        return null;
    }
//    获取图片路径



}
